﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Leo.Blog.BLL.SysModule;
using Leo.Blog.Common.JsonHelper;
using Leo.Blog.ViewModel;
using Leo.Blog.ViewModel.SysModule;

namespace Leo.Blog.Controllers
{
    public class ModuleManageController : BaseController
    {
        // GET: SysModule
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult GetSysModules(string t, int parentId = -1, int page = 1, int rows = 30)
        {
            int totalCount = 0;
            List<SysModuleVM> listModules = Sys_Modules_B.GetSysModules(page, rows, out totalCount, parentId);
            var obj = new
            {
                total = totalCount,
                rows = listModules,
            };
            var toJson = JsonHelper.SerializeObject(obj);
            return Content(toJson);
        }
        /// <summary>
        /// 增加和修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add(int id)
        {
            SysModuleVM model = Sys_Modules_B.FindSingle(id);
            if (model == null)
            {
                model = new SysModuleVM();
            }
            return View(model);
        }
        //增加或修改对象
        [HttpPost]
        public ActionResult Add(SysModuleVM model)
        {//修改父节点的路径需要使子节点的路径随之变化
            if (model.ParentModuleId == 0)
            {
                model.ModulePath = "0";//model.ModuleId + "";
            }
            else
            {
                SysModuleVM modelParent = Sys_Modules_B.FindSingle(model.ParentModuleId);
                model.ModulePath = modelParent.ModulePath + "-" + modelParent.ModuleId;
            }
            model.CreateBy = this.UserId;
            SysModuleVM obj = Sys_Modules_B.AddorUpdate(model);
            if (obj != null)
            {
                return Json(ResCode.Success);
            }
            return Json(ResCode.Fail);
        }
        /// <summary>
        /// 删除，以","区分开
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string ids)
        {
            if (Sys_Modules_B.DeleteList(ids))
            {
                return Json(ResCode.Success);
            }
            return Json(ResCode.Fail);
        }
        [HttpPost]
        public ActionResult UpdateIsEnable(int moduleId)
        {
            bool res = Sys_Modules_B.UpdateIsEnable(moduleId, this.UserId);
            if (res)
            {
                return Json(ResCode.Success);
            }
            return Json(ResCode.Fail);
        }
        //[HttpPost]
        /// <summary>
        /// 获取所有模组信息生成树
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetSysModulesTree(string t, int parentId = 0)
        {
            //easyui树的json对象得是一个数组
            TreeNode rootNode = new TreeNode();
            rootNode.Id = 0;
            rootNode.State = "open";
            rootNode.Text = "根节点";
            //rootNode.IconName = "icon-save";
            Sys_Modules_B.GetTreeByAllList(rootNode, parentId);
            //List<TreeNode> nodeList = new List<TreeNode> { rootNode };
            var toJson = JsonHelper.SerializeObject(rootNode);
            return Content("[" + toJson + "]");//需要将rootNode生成的对象加[]变为数组
        }
        /// <summary>
        /// 获取所有有效模组信息生成树
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetSysModulesTreeIsEnable(string t, int parentId = 0)
        {
            TreeNode rootNode = new TreeNode();
            rootNode.Id = 0;
            rootNode.State = "open";
            rootNode.Text = "根节点";
            //rootNode.IconName = "";

            Sys_Modules_B.GetTreeByAllIsEnable(rootNode, parentId);
            var toJson = JsonHelper.SerializeObject(rootNode);
            return Content("[" + toJson + "]");
        }

        public ActionResult ImportData()
        {
            return Content("");
        }
    }
}
